package training.bms.presentation;

import java.util.List;

import javax.faces.bean.ManagedBean;

import training.bms.business.Blog;
import training.bms.business.BlogController;
import training.bms.business.Post;
import training.bms.business.PostController;
import training.bms.business.PostSearchOptions;

@ManagedBean
public class ShowBlog {
	
	private Integer blogIdentifier;
	private Blog blog;
	private List<Post> posts;

	public void setBlogIdentifier(Integer blogIdentifier) {
		this.blogIdentifier = blogIdentifier;
		
		BlogController controller = new BlogController();
		blog = controller.searchEntity(blogIdentifier);
		
		PostSearchOptions options = new PostSearchOptions();
		options.setBlogIdentifier(blogIdentifier);
		options.setMaxResults(10);
		PostController postController = new PostController();
		posts = postController.searchEntity(options);
	}
	
	public Integer getBlogIdentifier() {
		return blogIdentifier;
	}
	
	public Blog getBlog() {
		return blog;
	}

	public List<Post> getPosts() {
		return posts;
	}

	public void setPosts(List<Post> posts) {
		this.posts = posts;
	}
	
	public String getFirstParagraph(Post post){
		StringBuilder paragraph = new StringBuilder();
		String content = post.getContent();
		
		for(int i = 0; i < content.length(); i++){
			if(content.charAt(i) == '\r' || content.charAt(i) == '\n'){
				break;
			}
			paragraph.append(content);
		}
		return paragraph.toString();
	}
}
